# -*- Makefile -*- 
#
#
# Copyright (C) 2006 Pat Thoyts <patthoyts@users.sourceforge.net>
#
#-------------------------------------------------------------------------
# $Id$
#-------------------------------------------------------------------------

VERSION=0.4

# You should set SDLDIR to the directory containing your SDL include
# and lib directories.
!ifndef SDLDIR
SDLDIR    =/opt
!endif

#-------------------------------------------------------------------------
# There should be no need to edit below this point.
#-------------------------------------------------------------------------

SRCDIR =src

!ifndef DEBUG
DEBUG   =0
!endif
!ifndef PROFILE
PROFILE =0
!endif
!ifndef SYMBOLS
SYMBOLS = 0
!endif
!ifndef CC
CC=cl
!endif
!ifndef LINK
LINK=link
!endif

!if $(DEBUG)
OUTDIR =Debug
CFLAGS =-Od -Zi -GZ -MDd -D_DEBUG -DDEBUG
LDFLAGS=-debug:full -debugtype:cv
!else
OUTDIR =Release
!if $(SYMBOLS)
CFLAGS =-Od -Zi -Op -Gs -MD -DNDEBUG
LDFLAGS=-debug -opt:ref -opt:icf,3
!else
CFLAGS =-O2 -Otip -Gs -MD -DNDEBUG
LDFLAGS=-release -opt:ref -opt:icf,3
!endif
!endif

!if $(PROFILE)
CFLAGS =$(CFLAGS) -Zi
LDFLAGS=$(LDFLAGS) -profile -map
!endif

!if "$(OS)" == "Windows_NT"
RMDIR = rmdir /s /q >NUL
!else
RMDIR = deltree /y
!endif
DEL   = del /f /q
CP    = copy /y >NUL
MAKENSIS = "C:\Program Files (x86)\NSIS\makensis.exe"

TMPDIR =$(OUTDIR)\Objects
TSTDIR =$(OUTDIR)\TestObjects

CC     =$(CC) -nologo
LD     =$(LINK) -nologo

CFLAGS =$(CFLAGS) -W3 -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE
INC    =-I$(SDLDIR)/include
DEFS   =-DWIN32 -DHAVE_OPENGL -Dmain=SDL_main
LIBS   =-libpath:$(SDLDIR)/lib SDLmain.lib SDL.lib SDL_mixer.lib user32.lib kernel32.lib
LDFLAGS=$(LDFLAGS) -subsystem:windows

OBJS   = \
        $(TMPDIR)\ag.obj \
        $(TMPDIR)\ag_core.obj \
        $(TMPDIR)\dlb.obj \
        $(TMPDIR)\linked.obj \
        $(TMPDIR)\sprite.obj \
	$(TMPDIR)\ag.res

all:    ag
ag:     setup $(OUTDIR)\ag.exe

$(OUTDIR)\ag.exe: $(OBJS)
	$(LD) $(LDFLAGS) -out:$@ $** $(LIBS)

release: ag Anagramarama.nsi
	@$(CP) $(SDLDIR:/=\)\bin\sdl.dll $(OUTDIR)\SDL.dll
	@$(CP) $(SDLDIR:/=\)\bin\SDL_mixer.dll $(OUTDIR)\SDL_mixer.dll
	@$(MAKENSIS) /V2 Anagramarama.nsi
	@echo Installer file created

.PHONY: all ag release

#-------------------------------------------------------------------------

tests: test_agcore test_dlb test_linked
test_ag: setup $(OUTDIR)\test_ag.exe
test_dlb: setup $(OUTDIR)\test_dlb.exe
test_agcore: setup $(OUTDIR)\test_agcore.exe
test_linked: setup $(OUTDIR)\test_linked.exe

$(OUTDIR)\test_ag.exe: $(SRCDIR)/ag_test.c
	$(CC) $(CFLAGS) $(INC) -DUNIT_TEST -Fo$(TSTDIR)\ $** -link $(LDFLAGS) -subsystem:console -out:$@

$(OUTDIR)\test_agcore.exe: $(SRCDIR)/ag_core.c $(TMPDIR)\dlb.obj $(TMPDIR)\linked.obj
	$(CC) $(CFLAGS) $(INC) -DUNIT_TEST -Fo$(TSTDIR)\ $** -link $(LDFLAGS) -subsystem:console -out:$@

$(OUTDIR)\test_linked.exe: $(SRCDIR)/linked.c
	$(CC) $(CFLAGS) $(INC) -DUNIT_TEST -Fo$(TSTDIR)\ $** -link $(LDFLAGS) -subsystem:console -out:$@

$(OUTDIR)\test_dlb.exe: $(SRCDIR)/dlb.c
	$(CC) $(CFLAGS) $(INC) -DUNIT_TEST -Fo$(TSTDIR)\ $** -link $(LDFLAGS) -subsystem:console -out:$@


#-------------------------------------------------------------------------
setup:
	@if not exist $(OUTDIR) mkdir $(OUTDIR)
	@if not exist $(TMPDIR) mkdir $(TMPDIR)
	@if not exist $(TSTDIR) mkdir $(TSTDIR)

clean:
	@if exist $(TMPDIR)\NUL $(RMDIR) $(TMPDIR) >NUL

realclean: clean
	@if exist $(OUTDIR)\NUL $(RMDIR) $(OUTDIR) >NUL

.PHONY: setup clean realclean
#-------------------------------------------------------------------------

.SUFFIXES:.c .cpp .rc

{$(SRCDIR)}.c{$(TMPDIR)}.obj::
	$(CC) $(CFLAGS) $(DEFS) $(INC) -Fo$(TMPDIR)\ -c @<<
$<
<<

{$(SRCDIR)}.cpp{$(TMPDIR)}.obj::
	$(CC) $(CFLAGS) $(DEFS) $(INC) -Fo$(TMPDIR)\ -c @<<
$<
<<

{$(SRCDIR)}.rc{$(TMPDIR)}.res:
	$(RC) -fo $@ -r -D__WIN32__ \
	-DCOMMAVERSION=$(VERSION:.=,),0,0 \
	-DVERSION=\"$(VERSION)\" \
!if $(DEBUG)
	-d DEBUG \
!endif
	$<

#-------------------------------------------------------------------------
